<template>
  <el-dialog
    :title="$t('customer.setupTag')"
    :visible.sync="dialogVisible"
    width="500px"
    :show="show"
    append-to-body
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @open="open"
  >
    <el-form
      ref="form"
      :model="info"
      label-width="90px"
      :rules="rules"
      v-loading="infoLoading"
    >
      <div class="el-dialog-div form-scrollbar">
        <el-form-item :label="$t('common.tag')" prop="label">
          <el-select
            v-model="info.label"
            class="filter-left"
            :placeholder="$t('common.select', [$t('common.tag')])"
            clearable
            filterable
            multiple
            size="small"
          >
            <el-option
              v-for="item in options"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-row>
        <el-col :span="12" class="btnEdit">
          <el-button :disabled="infoLoading" type="primary" @click="subInfo">{{
            $t('permission.confirm')
          }}</el-button>
        </el-col>
        <el-col :span="12" class="btnEdit">
          <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
        </el-col>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import { getLabelCollect } from '@/api/customer/label'
import { getCustomer, setLabelCollect } from '@/api/customer'
import { setBlockLabelCollect } from '@/api/customer/block'

export default {
  name: 'Label',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    id: '',
    closeAction: '',
    source: {
      type: String,
      default: 'customer',
    },
  },
  data() {
    return {
      dialogVisible: this.show,
      infoLoading: true,
      options: [],
      info: {
        label: [],
      },
      rules: {
        //element form表单元素验证
        label: [
          {
            required: true,
            message: this.$t('common.select', [this.$t('common.tag')]),
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    async open() {
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      const { data } = await getLabelCollect()
      this.options = data
      this.info.label = []
      if (typeof this.id != 'object') {
        let { data } = await getCustomer(this.id)
        let labels = data.labels
        if (labels != null) {
          for (let i in labels) {
            this.info.label.push(labels[i]['id'])
          }
        }
      }
      setTimeout(() => {
        this.infoLoading = false
      }, 0.5 * 1000)
    },
    subInfo() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          this.infoLoading = true
          let data = {
            label: this.info.label,
            ids: this.id,
          }
          if (typeof data.ids != 'object') {
            data.ids = [data.ids]
          }
          if (this.source === 'customer') {
            await setLabelCollect(data)
              .then((res) => {
                this.$notify({
                  title: this.$t('common.success'),
                  message: this.$t('common.updateSuccessfully'),
                  type: 'success',
                  duration: 2000,
                })
                this.$emit('closeAction')
              })
              .catch(() => {
                setTimeout(() => {
                  this.infoLoading = false
                }, 0.5 * 1000)
              })
          } else if (this.source === 'block') {
            await setBlockLabelCollect(data)
              .then((res) => {
                this.$notify({
                  title: this.$t('common.success'),
                  message: this.$t('common.updateSuccessfully'),
                  type: 'success',
                  duration: 2000,
                })
                this.$emit('closeAction')
              })
              .catch(() => {
                setTimeout(() => {
                  this.infoLoading = false
                }, 0.5 * 1000)
              })
          }
        }
      })
    },
    handleClose(done) {
      this.$emit('update:show', false)
    },
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    },
  },
}
</script>

<style scoped>
.form-scrollbar {
  height: 200px !important;
}
</style>
